<template>
  <div class="app-container">
    <div class="hint" v-if="disconnect">
      <div style="display:flex">
        断网自助机
        <span style="font-size:15px">{{ this.breakStation }}</span
        >个
        <el-button
          class="btn"
          style="background:none"
          v-hasPermi="['station:selfService:edit']"
          @click="getErro"
          >查看</el-button
        >
      </div>
      <div @click="close">
        <el-button class="btn" style="background:none;color:#999"
          >不在提醒</el-button
        >
      </div>
    </div>
    <el-row :gutter="10" class="mb8" style="margin-top:20px">
      <el-col :span="1.5">
        <el-button
          type="primary"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['station:atmAllocation:edit']"
          >添加</el-button
        >
      </el-col>
      <!-- <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar> -->
    </el-row>

    <el-table v-loading="loading" :data="postList">
      <el-table-column width="70" type="index" label="序号" align="center" />
      <el-table-column label="自助机编号" align="center" prop="machinecode" />
      <el-table-column label="工号" align="center" prop="userId" />
      <el-table-column label="密码" align="center" prop="pwd" />
      <el-table-column label="当前票号" align="center" prop="dqph" />
      <el-table-column label="起票号" align="center" prop="qph" />
      <el-table-column label="止票号" align="center" prop="zph" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            v-hasPermi="['station:atmAllocation:edit']"
            @click="handleUpdate(scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            v-hasPermi="['station:atmAllocation:edit']"
            @click="handleDelete(scope.row)"
            >新增票号</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageIndex"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加/编辑配置 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="18">
            <el-form-item
              label="自助机编号:"
              prop="machinecode"
              label-width="115px"
            >
              <el-input
                @blur="getDetails"
                :disabled="atmNumber"
                v-model="form.machinecode"
                placeholder="请输入"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="18">
            <el-form-item label="工号:" prop="userId" label-width="115px">
              <el-input
                :disabled="jobNum"
                v-model="form.userId"
                placeholder="请输入"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="18">
            <el-form-item label="登录密码:" prop="pwd" label-width="115px">
              <el-input
                :disabled="password"
                v-model="form.pwd"
                placeholder="请输入"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="18">
            <el-form-item label="当前票号:" prop="dqph" label-width="115px">
              <el-input v-model="form.dqph" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="18">
            <el-form-item label="起票号:" prop="qph" label-width="115px">
              <el-input v-model="form.qph" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="18">
            <el-form-item label="止票号:" prop="zph" label-width="115px">
              <el-input v-model="form.zph" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="complie" type="primary" @click="complieForm"
          >确 定</el-button
        >
        <el-button v-if="submit" type="primary" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 新增票号 -->
    <el-dialog
      :title="title"
      :visible.sync="configuration"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="18">
            <el-form-item
              label="自助机编号:"
              prop="machinecode"
              label-width="115px"
            >
              <el-input v-model="form.machinecode" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="18">
            <el-form-item label="当前票号:" prop="dqph" label-width="115px">
              <el-input v-model="form.dqph" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="18">
            <el-form-item label="起票号:" prop="qph" label-width="115px">
              <el-input v-model="form.qph" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="18">
            <el-form-item label="止票号:" prop="zph" label-width="115px">
              <el-input v-model="form.zph" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="getSubmitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getAllPage,
  machine,
  ATMTicket,
  add,
  modify,
  getCount
} from "@/api/station/atmAllocation";
export default {
  name: "AtmAllocation",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      statusOptions: [],
      //自助机配置弹出框
      configuration: false,
      // 控制自助机编号是否可编辑
      atmNumber: false,
      // 断网自助机数量
      breakStation: "",
      disconnect: false, //自助机弹窗显示
      // 控制是新增还是编辑
      complie: false,
      submit: false,
      jobNum: false,
      password: false,
      // 查询参数
      queryParams: {
        pageIndex: 1,
        pageSize: 10
      },
      isallowVal: "",
      loginVal: "",
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        machinecode: [
          { required: true, message: "自助机编号不能为空", trigger: "blur" }
        ],
        qph: [{ required: true, message: "起票号不能为空", trigger: "blur" }],
        zph: [{ required: true, message: "止票号不能为空", trigger: "blur" }],
        dqph: [
          { required: true, message: "当前票号不能为空", trigger: "blur" }
        ],
        pwd: [{ required: true, message: "密码不能为空", trigger: "blur" }],
        userId: [{ required: true, message: "工号不能为空", trigger: "blur" }]
      }
    };
  },
  created() {
    this.getList(); //自助机配置列表
    this.getState(); //自助机错误
  },
  methods: {
    // 表单重置
    reset() {
      this.form = {
        machinecode: undefined,
        id: undefined,
        qph: undefined,
        zph: undefined,
        dqph: undefined,
        globalcode: undefined,
        terminalcode: undefined,
        pwd: undefined,
        userId: undefined,
        isallow: undefined,
        islogin: undefined
      };
      // this.resetForm("form");
    },
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      getAllPage(this.queryParams).then(response => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 查询自助机状态
    getState() {
      getCount().then(response => {
        this.breakStation = response.data.errorCount;
        if (this.breakStation != 0) {
          this.disconnect = true;
        }
      });
    },
    // 查看按钮
    getErro() {
      // 跳转到详情页面
      this.$router.push({
        path: "/operationLog/selfHelpErro"
      });
    },
    //不再提醒按钮
    close() {
      this.disconnect = false;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.configuration = false;
      this.reset();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.atmNumber = false;
      this.open = true;
      this.title = "添加配置";
      this.$nextTick(() => {
        this.$refs["form"].clearValidate();
      });
      // 显示新增按钮 隐藏编辑
      this.complie = false;
      this.submit = true;
      this.jobNum = false;
      this.password = false;
      this.reset(); // 清空参数
    },
    // 获取新增的其它参数
    getDetails() {
      machine({ machinecode: this.form.machinecode }).then(response => {
        if (response.code == 200) {
          this.form.globalcode = response.data.globalcode;
          this.form.terminalcode = response.data.terminalcode;
          this.form.isallow = response.data.isallow;
          this.form.islogin = response.data.islogin;
          if (response.data.userId != null) {
            this.form.userId = response.data.userId;
            this.jobNum = true;
          } else {
            this.jobNum = false;
          }
          if (response.data.pwd != null) {
            this.form.pwd = response.data.pwd;
            this.password = true;
          } else {
            this.password = false;
          }
        } else {
          this.$message.error(response.msg);
        }
      });
    },
    /** 编辑按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = row;
      this.open = true;
      this.title = "修改自助机";
      this.$nextTick(() => {
        this.$refs["form"].clearValidate();
      });
      this.atmNumber = true;
      this.jobNum = true;
      this.password = true;
      // 显示编辑按钮 隐藏新增
      this.submit = false;
      this.complie = true;
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          ATMTicket(this.form).then(response => {
            if (response.code == 200) {
            } else {
              this.$message.error(response.msg);
            }
          });
          add(this.form).then(response => {
            if (response.code == 200) {
              this.open = false;
              this.getList();
            } else {
              this.$message.error(response.msg);
            }
          });
        }
      });
    },
    //编辑确定按钮
    complieForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          modify(this.form).then(response => {
            if (response.code == 200) {
              this.open = false;
              this.getList();
            } else {
              this.$message.error(response.msg);
            }
          });
        }
      });
    },
    /** 新增票号按钮*/
    handleDelete(row) {
      this.reset();
      this.title = "新增票号";
      this.configuration = true;
      this.$nextTick(() => {
        this.$refs["form"].clearValidate();
      });
      this.form.machinecode = row.machinecode;
      this.form.id = row.id;
    },
    //  新增票号请求
    getSubmitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          ATMTicket(this.form).then(response => {
            if (response.code == 200) {
              this.getList();
              this.configuration = false;
            } else {
              this.$message.error(response.msg);
            }
          });
        }
      });
    }
  }
};
</script>
<style scoped>
.hint {
  width: 80%;
  height: 30px;
  padding: 0 10px;
  border: 1px solid red;
  line-height: 30px;
  font-size: 13px;
  color: #999;
  border-radius: 5px;
  display: flex;
  justify-content: space-between;
  background-color: #ffe4e1;
}
.btn {
  background: none;
  border: none;
  height: 0px;
  color: blue;
  margin-top: -2px;
  margin-left: -15px;
}
</style>
